<?php

if (!function_exists('is_mobile')) {

    /**
     * 检查是否为手机号
     *
     * @param string $content
     * @return bool
     */
    function is_mobile($content = '')
    {
        return (bool)preg_match("/^1\d{10}$/", $content);
    }
}

if (!function_exists('attachment_out')) {
    /**
     * 附件出口转换
     *
     * 将附件中的占位符替换为URL前缀以得到可访问的完整URL
     *
     * @param string $content
     * @return mixed|string
     */
    function attachment_out($content)
    {
        if (!is_string($content) || $content == '') {
            return '';
        }
        return str_replace(config('filesystems.disks.public')['storage_placeholder'], config('app.url'), $content);
    }
}

if (!function_exists('attachment_in')) {
    /**
     * 附件URL入口转换
     *
     * 将附件中的URL前缀替换为占位符进行存储
     *
     * @param string $content
     * @return mixed|string
     */
    function attachment_in($content)
    {
        if (!is_string($content) || $content == '') {
            return '';
        }

        return str_replace(config('app.url'), config('filesystems.disks.public')['storage_placeholder'], $content);
    }
}


if (!function_exists('get_user_roles')) {
    /**
     *
     *
     * 获取网站配置
     *
     * @param string $key
     * @return mixed|string
     */
    function get_user_roles()
    {
        static $roles;
        if (!$roles) {
            $roles = \App\Models\UserRole::query()->get()->keyBy('id');
        }
        return $roles;
    }
}

if (!function_exists('get_user_role')) {
    /**
     * 获取角色名称
     *
     * @param $id
     * @return mixed|string
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/17 00:27
     */
    function get_user_role($id)
    {
        static $roles;
        if (!$roles) {
            $roles = \App\Models\UserRole::query()->get()->pluck('name', 'id');
        }
        return $roles[$id] ?? '';
    }
}

if (!function_exists('get_user_by_role')) {
    /**
     * 根据角色获取用户信息
     * @param array $role_name
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/17 21:33
     */
    function get_user_by_role(array $role_name)
    {
        return \App\Models\User::query()->leftJoin('user_roles', 'user_roles.id', 'users.role_id')->whereIn('alias', $role_name)->select(['users.id', 'users.name'])->get();
    }
}

if (!function_exists('get_user')) {
    /**
     * 获取用户信息
     *
     * @param int $id
     * @param string $field
     * @return \App\Models\User|mixed|null
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/17 22:22
     */
    function get_user(int $id, string $field = '*')
    {
        static $users;
        if (!isset($users)) {
            $users = \App\Models\User::all()->keyBy('id');
        }
        if ('*' == $field || '' == $field) {
            return $users[$id] ?? null;
        }
        return $users[$id][$field] ?? null;
    }
}

if (!function_exists('get_users')) {
    /**
     * 获取所以用户信息
     * @return \App\Models\User[]|\Illuminate\Database\Eloquent\Collection|mixed
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/18 16:16
     */
    function get_users()
    {
        static $users;
        if (!isset($users)) {
            $users = \App\Models\User::query()->where('enabled', 1)->get()->keyBy('id');
        }

        return $users;
    }
}

if (!function_exists('get_project_types')) {
    /**
     * 获取用户信息
     *
     * @param int $id
     * @param string $field
     * @return \App\Models\User|mixed|null
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/17 22:22
     */
    function get_project_types()
    {
        return \App\Models\Project\ProjectType::all()->keyBy('id');
    }
}

if (!function_exists('get_payment_terms')) {
    /**
     * 获取付款条件
     * @param int $id
     * @param string $field
     * @return \App\Models\Project\PaymentTerm[]|\Illuminate\Database\Eloquent\Collection|mixed|string
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/18 21:28
     */
    function get_payment_terms()
    {
        static $payment_terms;
        if (!isset($payment_terms)) {
            $payment_terms = \App\Models\Project\PaymentTerm::all()->keyBy('id');
        }
        return $payment_terms;
    }
}

if (!function_exists('get_payment_term')) {
    /**
     * 获取付款条件
     * @param int $id
     * @param string $field
     * @return \App\Models\Project\PaymentTerm[]|\Illuminate\Database\Eloquent\Collection|mixed|string
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/18 21:28
     */
    function get_payment_term(int $id, string $field = '*')
    {
        static $payment_terms;
        if (!isset($payment_terms)) {
            $payment_terms = \App\Models\Project\PaymentTerm::all()->keyBy('id');
        }
        if ('*' == $field || '' == $field) {
            return $payment_terms[$id] ?? null;
        }
        return $payment_terms[$id][$field] ?? null;
    }
}

if (!function_exists('get_sign_companies')) {
    /**
     * 获取付款条件
     * @param int $id
     * @param string $field
     * @return \App\Models\Project\PaymentTerm[]|\Illuminate\Database\Eloquent\Collection|mixed|string
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/18 21:28
     */
    function get_sign_companies()
    {
        static $sign_companies;
        if (!isset($sign_companies)) {
            $sign_companies = \App\Models\SignCompany::all()->keyBy('id');
        }
        return $sign_companies;
    }
}

if (!function_exists('get_sign_company')) {
    /**
     * 获取付款条件
     * @param int $id
     * @param string $field
     * @return \App\Models\Project\PaymentTerm[]|\Illuminate\Database\Eloquent\Collection|mixed|string
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/18 21:28
     */
    function get_sign_company(int $id)
    {
        static $sign_companies;
        if (!isset($sign_companies)) {
            $sign_companies = \App\Models\SignCompany::all()->keyBy('id');
        }

        return $sign_companies[$id]['name'] ?? null;
    }
}
if (!function_exists('get_external_companies')) {
    /**
     * 获取外部公司
     * @param string $category
     * @return \App\Models\ExternalContact|array|mixed
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/19 17:28
     */
    function get_external_companies(string $category = 'other_company')
    {
        static $companies;
        if (!isset($companies)) {
            $companies = \App\Models\ExternalContact::all()->groupBy('category');
        }
        return $companies[$category] ?? [];
    }
}

if (!function_exists('get_finance_categories')) {
    /**
     * 获取外部公司
     * @param string $category
     * @return \App\Models\ExternalContact|array|mixed
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/19 17:28
     */
    function get_finance_categories(string $type)
    {
        static $finance_categories;
        if (!isset($finance_categories)) {
            $finance_categories = \App\Models\FinanceCategory::query()->where('enabled', 1)->get();
        }
        if ($type == 'cost') {
            return $finance_categories->where('is_cost', 1);
        }
        if (!$type) {
            return $finance_categories;
        }
    }
}

if (!function_exists('get_progress_id')) {
    /**
     * 获取进度id
     * @param string $name
     * @return int|mixed
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/20 21:01
     */
    function get_progress_id(string $name)
    {
        static $progresses;
        if (!isset($progresses)) {
            $progresses = \App\Models\Project\Progress::all()->pluck('id','name')->toArray();
        }
        return $progresses[$name] ??0;
    }
}
if (!function_exists('get_progress')) {
    /**
     * 获取进度名称
     * @param $id
     * @return mixed|string
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/21 20:35
     */
    function get_progress($id)
    {
        static $progresses;
        if (!isset($progresses)) {
            $progresses = \App\Models\Project\Progress::all()->pluck('name','id')->toArray();
        }
        return $progresses[$id] ??'';
    }
}

if (!function_exists('get_linkage')) {
    /**
     * 根据id获取联动名称
     * @param $id
     * @return false|mixed
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/21 20:31
     */
    function get_linkage($id)
    {
        static $linkages;

        if (!$linkages) {
            $linkage_service = new \App\Services\LinkageService(new \App\Repositories\LinkageRepository());
            $linkages = $linkage_service->getLinkagesFromCache();
        }

        if (!isset($linkages[$id])) {
            return false;
        }
        return $linkages[$id]['name'];
    }
}


if (!function_exists('get_linkage_with_path')) {
    /**
     * 取得选项的全路径（[..>]爷>父>自己）
     *
     * @param $id
     * @param string $separator
     * @return false|string
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/21 20:31
     */
    function get_linkage_with_path($id, $separator = ' ')
    {
        static $linkages;

        if (!$linkages) {
            $linkage_service = new \App\Services\LinkageService(new \App\Repositories\LinkageRepository());
            $linkages = $linkage_service->getLinkagesFromCache();
        }

        if (!isset($linkages[$id])) {
            return false;
        }
        $arr = [];
        $arr[] = $linkages[$id]['name'];
        $pid = $linkages[$id]['pid'];
        $i = 0;
        while ($pid > 0) {
            if ($i > 5) {
                break;
            }
            if (!isset($linkages[$pid])) {
                break;
            }
            array_unshift($arr, $linkages[$pid]['name']);
            $pid = $linkages[$pid]['pid'];
            $i++;
        }
        return implode($separator, $arr);
    }
}
